package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.services.infrastructure.bean.EnterpriseBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.EnterpriseMapper;
import com.molichuxing.services.infrastructure.dto.request.create.PaymentEnterpriseCreateDto;
import com.molichuxing.services.infrastructure.dto.response.PaymentEnterpriseDto;
import com.molichuxing.services.infrastructure.service.PaymentEnterpriseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 企业订单支付
 * </p>
 *
 * @author zhaopenghui
 * @since 2020-06-05
 */
@Service("paymentEnterpriseService")
public class PaymentEnterpriseServiceImpl implements PaymentEnterpriseService {

    @Resource
    private EnterpriseMapper enterpriseMapper;

    /**
     * 支付分页
     *
     * @param params   orderCode：订单类型
     *                 enterpriseId:企业id
     * @param pageNum  页数
     * @param pageSize 每页条数
     * @return
     * @throws Exception
     */
    @Override
    public Paged<PaymentEnterpriseDto> getEnterprisePage(Map<String, Object> params, int pageNum, int pageSize) throws Exception {
        long pageCount = enterpriseMapper.getPageCount(params);
        if (pageCount <= 0) {
            return new Paged<>(null, 0, pageSize);
        }

        List<EnterpriseBean> beanList = enterpriseMapper.getPage(params, pageNum, pageSize);
        List<PaymentEnterpriseDto> dtoList = Convert.toEnterpriseDtoList(beanList);
        return new Paged<PaymentEnterpriseDto>(dtoList, pageCount, pageSize);
    }

    /**
     * 支付记录数
     *
     * @return
     */
    @Override
    public Integer getEnterpriseCount() {
        return enterpriseMapper.getEnterpriseCount();
    }

    /**
     * 根据id查询支付信息
     *
     * @param id
     * @return
     */
    @Override
    public PaymentEnterpriseDto getById(Integer id) {
        EnterpriseBean enterpriseBean = enterpriseMapper.getById(id);
        return Convert.toEnterpriseDto(enterpriseBean);
    }

    /**
     * 根据id查询支付信息
     *
     * @param scheduleId
     * @return
     */
    @Override
    public PaymentEnterpriseDto getByScheduleId(Integer scheduleId) {
        EnterpriseBean enterpriseBean = enterpriseMapper.getByScheduleId(scheduleId);
        return Convert.toEnterpriseDto(enterpriseBean);
    }

    /**
     * 新增企业订单支付信息
     *
     * @param createDto 新增信息
     * @return boolean
     */
    @Override
    public boolean create(PaymentEnterpriseCreateDto createDto) {
        return enterpriseMapper.create(Convert.toEnterpriseBean(createDto)) > 0;
    }

    /**
     * 查询企业订单支付信息
     *
     * @param scheduleIds 排期ids
     * @return
     */
    @Override
    public Map<Integer, PaymentEnterpriseDto> getEnterprise(List<Integer> scheduleIds) {
        List<EnterpriseBean> beans = enterpriseMapper.getByScheduleIds(scheduleIds);
        if (beans == null || beans.isEmpty()) {
            return null;
        }
        Map<Integer, PaymentEnterpriseDto> result = new HashMap<>();
        beans.forEach(bean -> {
            result.put(bean.getScheduleId(), Convert.toEnterpriseDto(bean));
        });

        return result;
    }

}
